<template>
  <div class="goodslist-container">
    <!-- <router-link :to="'/home/goodinfo/' + item.id" tag= "div" class="goods-item" v-for="item in datalist" :key="item.img_url">
      <img :src="item.img_url" alt title="图片" />
      <h1>{{item.title}}</h1>
      <div class="info">
        <p class="price">
          <span class="now">{{item.sell_price}}</span>
          <span class="old">{{item.market_price}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{item.stock_quantity}}件</span>
        </p>
      </div>
    </router-link>-->
    <!-- 这里是可以采用改造router-link的方式，但是学习一种新的编程式导航，通过router.push -->
    <div class="goods-item" v-for="item in datalist" :key="item.img_url" @click="toInfo(item.id)">
      <img :src="item.img_url" alt title="图片" />
      <h1>{{item.title}}</h1>
      <div class="info">
        <p class="price">
          <span class="now">{{item.sell_price}}</span>
          <span class="old">{{item.market_price}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{item.stock_quantity}}件</span>
        </p>
      </div>
    </div>
    <mt-button size="large" @click="getMore">加载更多</mt-button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      pageIndex: 1,
      datalist: []
    };
  },
  methods: {
    //获取数据
    getGoodList() {
      this.$http.get("getgoods?pageindex=" + this.pageIndex).then(result => {
        // console.log(result);
        if (result.data.status == 0) {
          this.datalist = this.datalist.concat(result.data.message);
        }
      });
    },
    getMore() {
      this.pageIndex++;
      this.getGoodList();
    },
    toInfo(id) {
      // 注意这个与this.$route不同，这个主要作为导航使用，跳转到新的url地址。而route储存的是路由中的参数
      this.$router.push("/home/goodinfo/" + id);
      //   注意，第二种方式，path不能与params同用，需要在routerjs中命名
      this.$router.push({ name: "goodinfo", params: { id: id } });
    }
  },
  created() {
    this.getGoodList();
  }
};
</script>
<style lang="scss">
.goodslist-container {
  display: flex;
  flex-wrap: wrap;
  .goods-item {
    width: 49%;
    border: 1px solid #ccc;
    box-shadow: 0 0 8px #ccc;
    margin: 3px 0;
    margin-left: 2.5px;
    // min-height: 242px !important;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    h1 {
      padding-left: 10px;
      font-size: 14px;
    }
    img {
      width: 100%;
      height: 200px;
    }
    .info {
      bottom: 0;
      padding-left: 10px;
      background-color: #eee;
      .price {
        padding-top: 10px;
        .now {
          color: red;
          font-weight: bold;
          font-size: 16px;
        }
        .old {
          text-decoration: line-through;
        }
      }
      .sell {
        margin: 0px;
        display: flex;
        justify-content: space-between;
      }
    }
  }
}
</style>